package com.zf.yichat.api.controller.user;

import com.zf.yichat.api.controller.BaseController;
import com.zf.yichat.api.dto.request.IntegralExchangeBalanceRequest;
import com.zf.yichat.api.dto.request.LowCarbonDetailsRequest;
import com.zf.yichat.api.dto.request.UserIntegralGetRequest;
import com.zf.yichat.model.User;
import com.zf.yichat.service.UserIntegralService;
import com.zf.yichat.service.UserService;
import com.zf.yichat.utils.response.FsPage;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import com.zf.yichat.vo.IntegralType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 签到积分
 *
 * @author fengsong
 * @date create in 10:36 2019/8/8 2019
 */
@RestController
@RequestMapping("user/integral")
public class UserIntegralController extends BaseController {


    @Autowired
    private UserService userService;

    @Autowired
    private UserIntegralService userIntegralService;

//    /**
//     * 获取积分接口
//     */
//    @PostMapping("user/integral/get")
//    public FsResponse sign(@RequestBody UserIntegralGetRequest params) {
//
//
//        return userIntegralService.getIntegral(IntegralType.valOf(params.getType()), getUserId(), params.getReferId());
//
//
//    }

    /**
     * 我的积分详情
     */
    @PostMapping("info")
    public FsResponse info() {

        return userIntegralService.info(getUserId());


    }

    /**
     * 我的积分详情列表
     */
    @PostMapping("info/list")
    public FsResponse list(@RequestBody FsPage params) {
        Long userId = getUserId();

        return userIntegralService.detailInfo(FsPage.init(params.getPageNo(), params.getPageSize()), userId);
    }

    /*********************碳汇林相关接口开始****************************************/
    /**
     * 初始化积分兑换余额页面-计算当前积分可兑换额度
     */
    @PostMapping("exchangeBalanceInitialization")
    public FsResponse exchangeBalanceInitialization() {
        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        return userIntegralService.exchangeBalanceInitialization(userId);
    }

    /**
     * 当前积分可兑换额度功能
     */
    @PostMapping("exchangeBalance")
    public FsResponse exchangeBalance(@RequestBody IntegralExchangeBalanceRequest request) {
        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        BigDecimal exchangeAmount = request.getExchangeAmount();
        if (null == exchangeAmount) {
            return FsResponseGen.failMsg("参数错误【exchangeAmount】");
        }

        return userIntegralService.exchangeBalance(userId, exchangeAmount);
    }

    /**
     * 当前积分可兑换额度记录
     */
    @PostMapping("exchangeBalanceRecordList")
    public FsResponse exchangeBalanceRecordList(@RequestBody IntegralExchangeBalanceRequest request) {
        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        Integer pageNo = request.getPageNo();
        if (null == pageNo) {
            pageNo = 1;
        }
        Integer pageSize = request.getPageSize();
        if (null == pageSize) {
            pageSize = 10;
        }

        return userIntegralService.exchangeBalanceRecordList(pageNo, pageSize, userId);
    }

    /**
     *查询用户兑换详情
     */
    @PostMapping("queryExchangeBalanceDetails")
    public FsResponse queryExchangeBalanceDetails() {
        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }


        return userIntegralService.queryExchangeBalanceDetails(userId);
    }

    /**
     *查询低碳明细
     */
    @PostMapping("queryLowCarbonDetails")
    public FsResponse queryLowCarbonDetails(@RequestBody LowCarbonDetailsRequest request) {
        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        Integer type = request.getType();
        String ctime = request.getCtime();
        Integer pageNo = request.getPageNo();
        if (null == pageNo) {
            pageNo = 1;
        }
        Integer pageSize = request.getPageSize();
        if (null == pageSize) {
            pageSize = 10;
        }

        return userIntegralService.queryLowCarbonDetails(FsPage.init(pageNo, pageSize), userId, type, ctime);
    }

    /**
     *查询累计兑换金额和累计获得的碳能量
     */
    @PostMapping("queryAccumulated")
    public FsResponse queryAccumulated() {
        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        return userIntegralService.queryAccumulated(userId);
    }


    /*********************碳汇林相关接口结束****************************************/
}
